package moe.bogos.douro.api.common.network.message;

import moe.bogos.douro.api.common.capabilities.CapMapProvider;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

import java.util.function.Supplier;


public class UpdateCap
{
    private CompoundNBT tagCompound;

    public UpdateCap(CompoundNBT tagCompound)
    {
        this.tagCompound = tagCompound;
    }

    public static void writeToPacket(UpdateCap message, PacketBuffer buffer)
    {
        buffer.writeNbt(message.tagCompound);
    }

    public static UpdateCap readFromPacket(PacketBuffer buffer)
    {
        return new UpdateCap(buffer.readNbt());
    }

    public static void handle(UpdateCap message, Supplier<NetworkEvent.Context> ctx)
    {
        ctx.get().enqueueWork(new Runnable() {
            public void run()
            {
                CapMapProvider.get(Minecraft.getInstance().player).loadNBTData(message.tagCompound);
            }
        });
        ctx.get().setPacketHandled(true);
    }
}
